﻿using Futajma.MigrationStudio.DataDefinition;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LLTreeManagementApplication.Data
{
    class SharePointStructureValidation
    {
        #region Public Static Methods
        // SharePoint validation check
        public static int ValidateTreeNode(TreeNode node)
        {
            int value = 0;

            DataNode dataNode = node.Tag as DataNode;

            #region Recursive tree iteration

            foreach (TreeNode child in node.Nodes)
            {
                dataNode.SharePointStructureViolationCollected += ValidateTreeNode(child);
            }

            value = dataNode.SharePointStructureViolationCollected;
            #endregion

            #region Validation
            if (node.Parent != null)
            {

                //DataNode dataNode = node.Tag as DataNode;
                DataNode parentDataNode = node.Parent.Tag as DataNode;

                switch (dataNode.SharePointType)
                {
                    case SharePointType.Document:
                    case SharePointType.Item:
                        if (parentDataNode.SharePointType != SharePointType.Folder
                            && parentDataNode.SharePointType != SharePointType.DocLibrary)
                        {
                            value++;
                            dataNode.SharePointStructureViolation = true;
                            //dataNode.ExcludeableFromMigration = true;
                        }
                        break;
                    case SharePointType.Folder:
                        if (parentDataNode.SharePointType != SharePointType.DocLibrary
                            && parentDataNode.SharePointType != SharePointType.Folder)
                        {
                            value++;
                            dataNode.SharePointStructureViolation = true;
                            //dataNode.ExcludeableFromMigration = true;
                        }
                        break;
                    case SharePointType.DocLibrary:
                        if (parentDataNode.SharePointType != SharePointType.Site
                            && parentDataNode.SharePointType != SharePointType.SiteCollection)
                        {
                            value++;
                            dataNode.SharePointStructureViolation = true;
                            //dataNode.ExcludeableFromMigration = true;
                        }
                        break;
                    case SharePointType.Site:
                        if (parentDataNode.SharePointType != SharePointType.SiteCollection
                            && parentDataNode.SharePointType != SharePointType.Site)
                        {
                            value++;
                            dataNode.SharePointStructureViolation = true;
                            //dataNode.ExcludeableFromMigration = true;
                        }
                        break;
                    case SharePointType.SiteCollection:
                        if (parentDataNode.SharePointType != SharePointType.Application)
                        {
                            value++;
                            dataNode.SharePointStructureViolation = true;
                            //dataNode.ExcludeableFromMigration = true;
                        }
                        break;
                }
            }

            #endregion

            //#region Node Coloring
            //if (value > 0)
            //{
            //    //if (dataNode == null || dataNode.Deletable == false)
            //    //{
            //    node.ForeColor = Color.Blue;
            //    //}
            //    //else
            //    //{
            //    //    node.ForeColor = Color.DarkBlue;
            //    //}
            //}
            //else
            //{
            //    //if (dataNode == null || dataNode.Deletable == false)
            //    //{
            //    node.ForeColor = SystemColors.ControlText;
            //    //}
            //    //else
            //    //{
            //    //    node.ForeColor = Color.DarkOrange;
            //    //}
            //}
            //#endregion

            return value;
        }

        //public static bool IsExcludeCheckBoxEnabled(DataNode dataNode)
        //{
        //    return dataNode.SharePointStructureViolation || dataNode.ExcludeableFromMigration;
        //}

        public static bool IsNodeMovable(DataNode dataNode)
        {
            return dataNode.SharePointStructureViolation;
        }

        public static void ResetValidation(TreeNode node)
        {
            foreach (TreeNode child in node.Nodes)
            {
                ResetValidation(child);
            }

            DataNode data = node.Tag as DataNode;

            data.GovernanceViolation = false;
            data.GovernanceViolationCollected = 0;
            data.ItemSizeExceededCount = 0;
            data.ItemThresholdExceededCount = 0;
            data.SharePointStructureViolation = false;
            data.SharePointStructureViolationCollected = 0;

            //if (data == null || data.Deletable == false)
            //{
            //node.ForeColor = SystemColors.ControlText;
            //}
            //else
            //{
            //    node.ForeColor = Color.DarkOrange;
            //}
        }
        #endregion

    }
}
